package neo.core.common;

import java.util.Map;
import neo.app.action.BaseAction;
import neo.core.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

public class AuthInterceptor extends BaseAction implements Interceptor {
	protected final Log log = LogFactory.getLog(getClass());

	public void destroy() {
	}

	public void init() {
	}
	
	public String intercept(ActionInvocation invocation) throws Exception {
		String nameSpace = invocation.getProxy().getNamespace();
		String actionName = invocation.getProxy().getActionName();
		String methodName = invocation.getProxy().getMethod();
		String authKey = nameSpace + "/" + actionName + "/" + methodName;
		Map dbUser = (Map) invocation.getInvocationContext().getSession().get(Constants.LOGIN_USER);
		Map userAuth = (Map) invocation.getInvocationContext().getSession().get(Constants.LOGIN_AUTH);
//	log.debug(actionName +" actionName");	
		if((Action) invocation.getAction() instanceof Anonymous || "login".equals(actionName) || "logout".equals(actionName)) {
			return invocation.invoke();
		}
//	log.debug(authKey +" ----");	
//	log.debug(userAuth +"userAuth");
		if(dbUser != null) {
			if(userAuth == null) {
//	log.debug("test1 auth_denied ");
				return "auth_denied";
			}
			String authString = (String) userAuth.get(authKey);
			if(authString == null || authString.equals("FALSE")) {
//	log.debug(authString +"test2 auth_denied");
				return "auth_denied";
			}
		}
		return invocation.invoke();
	}

}
